﻿using System;
using System.Threading;

namespace DeepMixWorkflow
{
    public class ThreadLocker
    {
        private readonly object _syncRoot = new object();

        public void Lock(Action action)
        {
            Lock();
            action();
            Unlock();
        }

        public T Lock<T>(Func<T> func)
        {
            Lock();
            var result = func();
            Unlock();
            return result;
        }

        public void Lock()
        {
            Monitor.Enter(_syncRoot);
        }

        public void Unlock()
        {
            Monitor.Exit(_syncRoot);
        }
    }
}